#!/usr/bin/env python
# -*- coding: utf-8 -*-

import urllib, re, sys

def getcitycode():
        conn=urllib.urlopen("http://weather.265.com/get_weather.php?action=get_city")
        citysrc=conn.read().decode("gbk").encode("utf8")
        conn.close()
        citycode=re.findall('[0-9]{5}', citysrc)[0]
        return citycode

def getweather(code):
        url="http://weather.265.com/weather/"+code+".htm"
        conn=urllib.urlopen(url)
        weasrc=conn.read().decode("gbk").encode("utf8")
        conn.close()
        wea=re.findall('show_weather\("(.*)"\)', weasrc)[0].split("'")
        weather=[item  for item in wea if item !="," and ")" not in item and "(" not in item]
        city=wea[0].split('"')[0]
        option=[u'今天:',u'上午:',u'下午:',u'晚上:',u'明天:',u'上午:',u'下午:',u'晚上:']
        #print "\n".join(weather)
        index=0
        print u'城市:',city
        while index < (len(option)):
                print option[index],weather[index]
                index=index+1

if __name__=='__main__':
        try:
                code=sys.argv[1]
        except:
                code=getcitycode()
        getweather(code) 
